var express = require('express');
var Router = express.Router();
//引入刚刚暴露出来的表模型
var musicModel = require('../models/music');
//定义添加到收藏接口
Router.post('/collect', function (req, res, next) {
    let musiCollect = new musicModel({
        song_name: req.body.song_name,
        author: req.body.author,
        time: req.body.time,
        song_id: req.body.song_id,
        // song_url: req.body.song_url
    });
    // 保存
    musicModel.findBySongId(req.body.song_id, function (err, song_id) {
        if (song_id.length != 0) {
            res.json({
                msg: "歌曲已收藏",
                type:"warning"

            })
        } else {
            musiCollect.save(function () {
                res.json({    
                    msg: "歌曲收藏成功",
                    type:"success"

                });
            });
        }
    });
});
// )
//查询收藏
Router.get('/list', function (req, res, next) {
    musicModel.find({}, function (err, doc) {
        res.json({
            states: 1,
            msg: doc //查询到的数据，以json格式返回
        });
    });
});
//删除收藏
Router.post('/del', function (req, res, next) {
    musicModel.remove({
        song_id: req.body.song_id
    }, function (err, doc) {
        {
            res.json({

            });
        }
    });

});

module.exports = Router; //暴露出路由